#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project : bigDataProject 
@File    : base_pos_api.py
@IDE     : PyCharm 
@Author  : Mario
@Date    : 2022/11/11 16:11 
@Describe: pos接口请求基类
"""
from typing import Text, Dict

from api.base_api import BaseAPI
from utils.time.time_control import get_timestamp


class BasePosAPI(BaseAPI):

    def __init__(self, brand_code: Text, env: Text) -> None:
        """
        构造函数
        :param brand_code: 品牌编码
        :param env: 环境
        """
        super().__init__()
        self.brand_code = brand_code
        self.env = env
        self.TYPE = "POS"
        credentials_sql = "SELECT url_prefix,token,device_mac,account,password FROM login_credentials WHERE " \
                          "environment = '{}' AND brand_code = '{}' AND type = '{}'".format(env, brand_code, self.TYPE)
        credentials_result = self.sqlite.query_data(credentials_sql)[0]
        self.URL = credentials_result[0]
        self.token = credentials_result[1]
        self.login_mac = credentials_result[2]
        self.short_no = credentials_result[3]
        self.password = credentials_result[4]

    def get_headers(self) -> Dict:
        timestamp = get_timestamp()
        headers = {
            'Connection': 'keep-alive',
            'retry_count': '1',
            'x-http-timestamp': timestamp,
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) ??POS?????/1.0.22 Chrome/87.0.4280.60 Electron/11.0.1 Safari/537.36',
            'retry': '4',
            'Accept': '*/*',
            'x-http-token': self.token,
            'x-http-osversion': '1.0.22',
            'x-http-request-id': self.login_mac + '-' + timestamp,
            'Accept-Language': 'zh-CN',
            'Cookie': 'SERVERID=k8s-ingress.gtmsh.com:10080; x-http-token=' + self.token,
            'Content-Type': 'application/json'
        }
        return headers


if __name__ == '__main__':
    test = BasePosAPI('823882', 'sit')
    url = 'table/list'
    payload = '{"operate":"0"}'
    test.http_request(url, payload, "台位列表")
